<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="采购效益分析编码">
                    <BaseInput
                      v-model="formData.code"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="项目名称"
                    prop="projectId"
                  >
                    <SelectDownProject
                      v-model="formData.projectId"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="工程名称"
                    prop="constructionTypeId"
                  >
                    <a-select
                      v-model="formData.constructionTypeId"
                      :disabled="state === 3"
                    >
                      <a-select-option
                        v-for="item in getProgrammeArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="起始时间"
                    prop="startDate"
                  >
                    <a-date-picker
                      v-model="formData.startDate"
                      :disabled="disabled"
                      value-format="YYYY-MM-DD HH:mm:SS"
                      @change="startDateChange"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="截止时间"
                    prop="endDate"
                  >
                    <a-date-picker
                      v-model="formData.endDate"
                      :disabled="disabled"
                      :disabled-date="disabledDate"
                      type="date"
                      value-format="YYYY-MM-DD HH:mm:SS"
                    />
                  </a-form-model-item>
                </a-col>

                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="24">
                  <a-form-model-item label="备注">
                    <BaseTextarea
                      v-model="formData.remark"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                v-show="!disabled"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="采购明细">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <materials
              :id="formData.id"
              ref="materials"
              :state="state"
              @getTotal="getTotal"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                :disabled="!formData"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[2]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :is-show="!disabled"
              :upload-list="addFile"
              :delete-url="fileDel"
              :list-url="fileList"
              :business-id="formData.id"
              business-code="MM05"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
          <div
      v-if="!isAudit"
      class="pub-header-btn page-btn-right-top"
    >
      <a-button
        v-if="state !== 3"
        type="primary"
        :loading="loading"
        @click="submit"
      >
        提交
      </a-button>
      <BasePrintButton
        v-if="formData.id && state == 3"
        :before-print-fn="beforePrintFn"
        style="display: inline-block"
      />
      <a-button
        v-checkVisibleInDetail
        @click="$close()"
      >
        关闭
      </a-button>
    </div>
     <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </base-breadcrumb>
  </div>
</template>

<script>

import benefitAnalysisApi from '@/api/material/benefitAnalysis/defalut'
import { selectOption } from '@/api/material/goods/goods'
import moment from 'moment'
import materials from './components/materials'
import { themeChoose } from '@/mixin/mixin'
import SelectDownProject from '@/components/Select/SelectDownProject'
import { LeasePrint } from '@/api/material/settlement/settlement'


export default {
	name:'a'+Date.now(),
    components: {
        materials,
        SelectDownProject
    },
    props: {
        isAudit: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            beforePrintFn: async () => {
                let res = await benefitAnalysisApi.print({ id: this.formData.id })
                return res.data.content
            },
            activeKey: [true, false, false, false, false, false, false],
            loading: false,
            state: Number(this.$route.query.state),
            isSubmit: false,
            formData: {
                endDate: null,
                createTime: this.getNowTime(),
                creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name
            },
            stepVal: 0,
            isNext: false,
            rules: {
                projectId: { required: true, message: '请选择项目名称', trigger: 'blur' },
                constructionTypeId: { required: true, message: '请选择工程名称', trigger: 'blur' },
                startDate: { required: true, message: '请选择起始时间', trigger: 'change' },
                endDate: { required: true, message: '请选择截止时间', trigger: 'change' }
            },
            addFile: benefitAnalysisApi.addFileUrl(),
            fileDel: benefitAnalysisApi.deleteFileUrl(),
            fileList: benefitAnalysisApi.findFileListUrl(),
            getProgrammeArr: [],
            //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },
    mounted: async function() {
        if (this.state !== 1) {
            this.formData.id = this.$route.query.id
            await this.getDetails()
            this.isSubmit = true
        }
        await this.getProgramme()
        //获取通过获取流程配置值
        this.processConfig = await this.$getConfigFn('mm_benefit_analysis')
    },
    watch: {
        stepVal(newVal, oldVal) {
            this.$set(this.activeKey, newVal, true)
        },
        isAudit: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.state = 3
                }
            }
        }
    },
    computed: {
        disabled() {
            return this.state === 3
        }
    },
    methods: {
        startDateChange() {
            this.formData.endDate = null
        },
        getTotal() {
        },
        disabledDate(current) {
            if (this.formData.startDate) {
                let startDate
                if (typeof this.formData.startDate === 'string') {
                    startDate = moment(this.formData.startDate)
                } else {
                    startDate = this.formData.startDate
                }
                return current && current < moment(this.formData.startDate)
            }
            return true
        },

        previous() {
            this.isNext = true
            this.stepVal--
            if (this.stepVal < 0) {
                this.stepVal = 0
            }
        },
        async projectChange(val) {
            this.project.map((item) => {
                if (item.code === val) {
                    this.formData.projectCode = item.code
                    this.formData.projectName = item.name
                }
            })
            this.$console(this.formData)
        },
        async getProgramme() {
            let res = await selectOption({ type: 18 })
            if (res.code === 200) {
                this.getProgrammeArr = res.data
            }
        },
        next(val) {
            this.isNext = true
            this.stepVal++
        },
        stepChange(val) {
            if (!this.formData.id && !this.isAudit) {
                if (val) {
                    this.$message.warning('切换步骤前请先填写基本信息')
                    this.stepVal = 0
                }
            } else {
                if (!this.isNext) {
                    this.$set(this.activeKey, val, !this.activeKey[val])
                }
            }
            this.isNext = false
        },
        getRow(val) {
            console.log(val)
            this.formData.fileReviewId = val.id
            this.formData.fileReviewCode = val.fileCode
            this.formData.wbsId = val.projectId
        },
        // 获取编辑回显数据
        async getDetails() {
            this.loading = true
            let res = await benefitAnalysisApi.findListByMainId({ id: this.formData.id })
            if (res.code === 200) {
                this.formData = this.$assign(this.formData, res.data)
            }
            this.loading = false
        },
        async submit() {
            this.setProcessConfig = null;
            if (this.isSubmit) {
                  this.submitApprovalConfirm(this.isSubmit?this.processConfig:6, async(flag)=>{
                      if(!flag) {
                        this.loading = false
                        return
                      }
                    this.loading = true
                    let res = await benefitAnalysisApi.submitApply({}, {
                        baseId: this.formData.id,
                        auditStatus: this.formData.auditStatus,
                        type: 0
                    })
                    if (res.code === 200) {
                        this.$message.success('提交成功')
                        //提交时，返回id
                        this.businessId = this.formData.id;
                        this.setProcessConfig = this.processConfig;
                        // this.$router.push({ path: '/material/benefitAnalysis/default' })
                    }
                    this.loading = false
                })
            } else {
                this.$message.warning('提交之前请先暂存')
            }
        },
        temporaryStorage(isNext) {
            this.$console(this.formData)
            if (this.state === 3) {
                this.next()
                return
            }
            this.$refs.ruleForm.validate(async valid => {
                if (valid) {
                    this.loading = true
                    if (this.state === 2 || this.formData.id) {
                        let res = await benefitAnalysisApi.editByMain({}, this.formData)
                        if (res.code === 200) {
                            this.$message.success('暂存成功')
                            this.isSubmit = true
                            if (isNext) {
                                this.next()
                            }
                        }
                    } else {
                        let res = await benefitAnalysisApi.createByMain({}, this.formData)
                        if (res.code === 200) {
                            this.$message.success('暂存成功')
                            this.isSubmit = true
                            if (isNext) {
                                this.next()
                            }
                            this.formData.id = res.data.id
                            await this.getDetails()
                        }
                    }
                    this.loading = false
                }
            })
        }
    }
}
</script>
<style scoped>
.tender-container .ant-radio-group {
    float: right;
}
</style>
